import * as _ from 'lodash'
import { join } from 'path'

import productionConfig from './prod.config'

export const isProd = process.env.NODE_ENV === 'production'

let config = {
  // app 启动信息
  app:{
    name: 'nest.js 项目实践',
    port: 8870,
    hostName: 'localhost',
    staticAssets: join(__dirname, '../public'),
    staticPrefix: '/public/',
    viewsDir: join(__dirname, '../views'),
    viewEngine: 'ejs',
  },

  // swagger 路由设置
  swagger:{
    urlPrefix: 'api-docs'
  },

  // session 设置
  session: {
    resave: true, // 重新保存：强制会话保存即使是未修改的。
    saveUninitialized: true, // 强制“未初始化”的会话保存到存储。
    secret: 'keyword',
    cookie: { maxAge: 60000 }
  },

  orm: {  
    // 存储数据库
    type: _.toString('mysql'), // 数据库类型
    host: 'localhost',
    port: _.toNumber(3306),
    username: 'root',
    password: 'root',
    database: 'nestjs',
    entities: [join(__dirname, '../../**/**.entity{.ts,.js}')],
    migrations: ['migration/*.ts'],
    timezone: 'UTC',
    charset: 'utf8mb4',
    multipleStatements: true,
    dropSchema: false,
    synchronize: true, // 此处为true才能自动建表
    logging: false,
    cache: { 
      // 缓存数据库
      type: _.toString('ioredis'),
      options: {
        host: 'localhost',
        port: 6379,
        family: 4,
        password: '',
        keyPrefix: '',
        db: 15
      },
    },
  },

  // jwt 验证信息
  jwt: {
    jwtFromRequest: "authorization", // header头标签
    secretOrKey: 'secretKey', // 秘钥
    signOptions: {
      expiresIn: '8h', // 超时时间
    },
  },
}

if (isProd) {
  config = _.merge(config, productionConfig)
}

export { config }
export default config